/*
* @Author: asus
* @Date:   2017-05-16 21:23:04
* @Last Modified by:   asus
* @Last Modified time: 2017-05-31 11:42:51
*/

'use strict';
require(["jquery-1.11.3","cookie"],function(){
	$(function(){

		//导航条下拉菜单
		$(".header_r>li").each(function(index, el) {
			$(this).hover(function() {
				$(this).find('.header_rs').css('display', 'block');
			}, function() {
				$(this).find('.header_rs').css('display', 'none');
			});
		});
		//表单框的验证	
		var oInput = document.getElementsByTagName("input");
		var oH6 = document.getElementsByTagName("h6");
		//表单验证
		//手机邮箱验证
		var re0 = /^.{5,20}$/;
		oInput[0].onblur = function(){
			if(oInput[0].value == ""){
				oH6[0].innerHTML = "不能输入为空，请重新输入";
			}
			else if(re0.test(oInput[0].value ) == false){
				oH6[0].innerHTML = "<i class = 'iconfont' >&#xe653;</i>请输入正确的邮箱/手机号";
				oH6[0].style.color = "red";
		 	}else{
		 		oH6[0].innerHTML = "<i class = 'iconfont' >&#xe654;</i>";
		 		oH6[0].style.color = "green";
		 	}
		};
		//密码验证
		var re1 = /^[A-Za-z0-9]{6,16}$/;
		oInput[1].onblur = function(){
			if(oInput[1].value == ""){
				oH6[1].innerHTML = "不能输入为空，请重新输入";
			}
			else if(re1.test(oInput[1].value ) == false){
		 		oH6[1].innerHTML = "<i class = 'iconfont' >&#xe653;</i>请输入正确的密码";
		 		oH6[1].style.color = "red";
		 	}else{
		 		oH6[1].innerHTML = "<i class = 'iconfont' >&#xe654;</i>";
		 		oH6[1].style.color = "green";
		 	}
		};
		//随机验证码
		function fn(){
			var arr=["0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"];
			var str="";
			for(var j =1;j <= 4;j ++){
				var a=Math.floor(Math.random()*62);
				 str+=arr[a];
				$(".code>span").html(str);
			}
		}
		fn();
		//验证验证码的输入是否正确
		oInput[2].onblur = function(){
			if(oInput[2].value == ""){
				oH6[2].innerHTML = "验证码不能为空";
			}
			else if(oInput[2].value !== $(".code>span").html()){
		 		oH6[2].innerHTML = "验证码输入有误，请重新输入";
		 	}else{
		 		oH6[2].innerHTML = "<i class ='iconfont'>&#xe654;</i>";
		 		oH6[2].style.color = "green";
		 	}
		};
		//ajax局部更新验证码
		//点击看不清，换一张进行局部刷新
		$("#refreshB").click(function(event) {
			$.ajax({
				url: "sasaRegisterRefresh.html",
				cache: false,
				success: function(refresh){
				 	$("#refreshSpan").html(refresh);
				}
			});
		});
		//点击验证码进行刷新
		$("#refreshSpan").click(function(event) {
			$.ajax({
				url: "sasaRegisterRefresh.html",
				cache: false,
				success: function(refresh){
				 	$("#refreshSpan").html(refresh);
				}
			});
		});
		//用户登录认证
		//点击登录按钮时进行验证
		var oLogin = document.getElementById("login");
		oLogin.onclick = function(){
			//获取用户输入的内容
			var _usename = oInput[0].value;
			var _password = oInput[1].value;
			//判断和cookie中获取的是否一致
			var _users = getCookie("registerUsers") ? getCookie("registerUsers") : "";
			//将字符串转为对象
			_users = convertStrToObj(_users);
			if(_users[_usename] == _password){
				setCookie("loginedUsers",_usename,7);
				alert("登录成功!");
				console.log(decodeURIComponent(document.cookie));
				//如果用户登录成功，登录注册信息将改成用户名
				var _users = getCookie("registerUsers") ? getCookie("registerUsers") : "";
				if(_users) {
					$(".header_l").html("您好！"+_usename+"<span style='color:orange'>0积分</span><a href = 'javascript:;' id='out' style='color:#ec3d7c'>");
				}
				// location.href = "../sasaIndex.html";
				// $(".header_l").html("您好！"+_usename+"<span style='color:orange'>0积分</span><a href = 'javascript:;' id='out' style='color:#ec3d7c'>");
			}else{
				alert("用户名和密码不匹配，请确认后重试！");
			}
			
		};
		
		
	});
	//将字符串转为对象
	function convertStrToObj(str){
		if(!str){ //如果是空字符串
			return {}; //返回空对象
		}
		var users = str.split(":");
		var obj = {};
		for(var i = 0; i < users.length; i ++){
			var userData = users[i].split(",");
			obj[userData[0]] = userData[1];
		}
		return obj;
	}

	//将对象转为字符串
	function convertObjToStr(obj){
		var str = "";
		//遍历对象
		for(var usn in obj){
			var pwd = obj[usn];
			if(str){
				str += ":";
			}
			str += usn + ',' + pwd;
		}
		return str;
	}

})
